<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />

<div type="hidden" id="message"></div>
<div class="content">
    <h1>Excused Absence Extensions</h1>
    <form id="extensions-form" class="exception-form" method="post" enctype="multipart/form-data" action="">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}" />
        <input type="hidden" name="option" value="-1" />
        <div class="option">
            <p>Use this form to grant an extension (e.g., for an excused absence) to a user on a specific assignment.</p>
            <div class="option">
                <label for="gradeable-select">Select Gradeable:</label>
                <select name="g_id" id="gradeable-select" onchange="updateGradeableForExtensions()">
                    <option disabled {% if current_gradeable is null %} selected {% endif %} value> -- select an option -- </option>
                    {% for index, value in gradeables %}
                        <option
                            value="{{value['g_id']}}"
                            {% if current_gradeable['g_id'] == value['g_id'] %} selected {% endif %}
                        >
                            {{ value['g_title'] }}
                        </option>
                    {% endfor %}
                </select>
                {% if current_gradeable is not null %}
                    <p id="due-date-label">Set Due Date:
                        <span id="due-date" data-date="{{ current_gradeable['eg_submission_due_date'] | date("Y-m-d") }}">
                            {{ current_gradeable['eg_submission_due_date'] | date("m/d/Y H:i:s") }}
                        <span>
                    </p>
                {% endif %}
            </div>
        </div>
         <input type="hidden" name="due_date" value="{{ due_date }}" />
        <fieldset class="single-student">
            <legend class="option-title">Single Student Entry</legend>
            <label for="user_id">
                Student ID:
                <input name="user_id" id="user_id" class="option-input" type="text">
            </label>
            <label for="late-days">
                Give the Days of Extension:
                <input name="late_days" id="late-days" class="option-input" type="text" onkeyup="clearDate()">
            </label>
            <label for="late-calendar">
                Or Pick a New Due Date:
                <input name="late_calendar" id="late-calendar" class="option-input" type="text" autocomplete="off" onchange="setLateDays()">
            </label>
            <a class="btn btn-primary" href="javascript:updateHomeworkExtension()">Submit</a>
        </fieldset>
        <fieldset>
            <legend class="option-title" id="csv-upload-title">Multiple Student Entry Via CSV Upload</legend>
            <label for="csv-upload" id="csv-upload-format">
                Do not use column headers. CSV must be of the following form: student_id, gradeable_id, days_of_extension
            </label>
            <input type="file" name="csv_upload" id="csv-upload" aria-labelledby="csv-upload-title csv-upload-format" onchange="updateHomeworkExtension()">
        </fieldset>
    </form>
    {% if current_exceptions is null %}
        <p id="empty-table" class="warning">
            {% if current_gradeable is null %}
                No gradeable has been selected
            {% else %}
                No extensions are currently entered for this gradeable
            {% endif %}
        </p>
    {% else %}
        {# This is a data table #}
        <table id="extensions-table" class="table table-striped mobile-table">
            <caption>Extensions</caption>
            <thead>
                <th>Student ID</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Number of Days of Extension</th>
                <th>Delete</th>
            </thead>
            <tbody>
                {% for user in current_exceptions %}
                    <tr>
                        <td class="align-left">{{ user.user_id }}</td>
                        <td class="align-left">{{ user.user_firstname }}</td>
                        <td class="align-left">{{ user.user_lastname }}</td>
                        <td>{{ user.late_day_exceptions }}</td>
                        <td>
                            <a
                                aria-label="Delete {{ user.getId() }}'s extension for {{ current_gradeable['g_id'] }}"
                                href="javascript:deleteHomeworkExtension('{{ user.user_id }}');"
                            >
                                <i class="fas fa-trash"></i>
                            </a>
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    {% endif %}
</div>
<script>
    $("#user_id").autocomplete({
        source: {{ student_full|raw }}
    });
    flatpickr("#late-calendar", {
        plugins: [ShortcutButtonsPlugin(
                {
                    button: [
                        {
                            label: "Now"
                        },
                        {
                            label: "End of time"
                        }
                    ],
                    label: "or",
                    onClick: (index, fp) => {
                        let date;
                        switch (index) {
                            case 0:
                                date = new Date();
                                break;
                            case 1:
                                date = new Date("01/01/9998");
                                break;
                        }
                        fp.setDate(date, true);
                    }
                }
            )],
        allowInput: true,
        dateFormat: "Y-m-d"
    });
</script>
{% include('admin/users/MoreExtensions.twig') %}
